package com.ruoyi.doc.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.ofbiz.OfbizBaseService;
import com.ruoyi.doc.domain.ContentType;
import com.ruoyi.doc.service.IContentTypeService;
import org.apache.ofbiz.base.util.UtilMisc;
import org.apache.ofbiz.service.GenericServiceException;
import org.apache.ofbiz.service.LocalDispatcher;
import org.apache.ofbiz.service.ServiceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 内容类型Service业务层处理
 *
 * @author yuanpingfang
 * @date 2021-03-30
 */
@Service
public class ContentTypeServiceImpl extends OfbizBaseService implements IContentTypeService {
    private static final Logger log = LoggerFactory.getLogger(ContentTypeServiceImpl.class);

    /**
     * 查询内容类型
     *
     * @param contentTypeId 内容类型ID
     * @return 内容类型
     */
    @Override
    public Map<String, Object> selectContentTypeById(String contentTypeId) {
        LocalDispatcher dispatcher = getDispatcher();
        Map<String, Object> returnMap = null;
        try {
            returnMap = dispatcher.runSync("selectContentTypeById", UtilMisc.toMap("contentTypeId", contentTypeId));
        } catch (GenericServiceException e) {
            log.error("selectContentTypeById Error", e);
            return ServiceUtil.returnError(e.getMessage());
        }
        return returnMap;
    }

    /**
     * 查询内容类型列表
     *
     * @param contentType 内容类型
     * @return 内容类型
     */
    @Override
    public Map<String, Object> selectContentTypeList(ContentType contentType, Integer pageNum, Integer pageSize, String orderBy) {
        LocalDispatcher dispatcher = getDispatcher();
        Map<String, Object> returnMap = null;
        Map<String, Object> context = new HashMap<>();
        context.put("contentType", contentType);
        context.put("pageNum", pageNum);
        context.put("pageSize", pageSize);
        context.put("orderBy", orderBy);
        try {
            returnMap = dispatcher.runSync("selectContentTypeList", context);
        } catch (GenericServiceException e) {
            log.error("selectContentTypeList Error", e);
            return ServiceUtil.returnError(e.getMessage());
        }
        return returnMap;
    }

    /**
     * 新增内容类型
     *
     * @param contentType 内容类型
     * @return 结果
     */
    @Override
    public Map<String, Object> insertContentType(ContentType contentType) {

        LocalDispatcher dispatcher = getDispatcher();
        Map<String, Object> returnMap = null;
        String jsonStr = JSONObject.toJSONString(contentType, false);
        try {
            Map<String, Object> context = JSONObject.parseObject(jsonStr, Map.class);
            returnMap = dispatcher.runSync("createContentType", context);
        } catch (GenericServiceException e) {
            log.error("createContentType Error", e);
            return ServiceUtil.returnError(e.getMessage());
        }
        return returnMap;
    }

    /**
     * 修改内容类型
     *
     * @param contentType 内容类型
     * @return 结果
     */
    @Override
    public Map<String, Object> updateContentType(ContentType contentType) {

        contentType.setCreatedStamp(null);
        contentType.setCreatedTxStamp(null);
        contentType.setLastUpdatedStamp(null);
        contentType.setLastUpdatedTxStamp(null);
        LocalDispatcher dispatcher = getDispatcher();
        Map<String, Object> returnMap = null;
        String jsonStr = JSONObject.toJSONString(contentType, false);
        try {
            Map<String, Object> context = JSONObject.parseObject(jsonStr, Map.class);
            returnMap = dispatcher.runSync("updateContentType", context);
        } catch (GenericServiceException e) {
            log.error("updateContentType Error", e);
            return ServiceUtil.returnError(e.getMessage());
        }
        return returnMap;
    }

    /**
     * 批量删除内容类型
     *
     * @param contentTypeIds 需要删除的内容类型ID
     * @return 结果
     */
    @Override
    public Map<String, Object> deleteContentTypeByIds(String[] contentTypeIds) {
        LocalDispatcher dispatcher = getDispatcher();
        Map<String, Object> returnMap = null;
        try {
            returnMap = dispatcher.runSync("deleteContentTypeByIds", UtilMisc.toMap("contentTypeIds", contentTypeIds));
        } catch (GenericServiceException e) {
            log.error("deleteContentTypeByIds Error", e);
            return ServiceUtil.returnError(e.getMessage());
        }
        return returnMap;
    }

}

